import random


def generate_winning_numbers():
    """
    生成一注中奖号码。
    """
    winning_numbers = random.sample(range(1, 34), 8)
    winning_numbers.sort()
    return winning_numbers


def check_lottery(user_numbers, winning_numbers):
    """
    检查用户输入的号码是否中奖。
    """
    correct_numbers = 0
    for number in user_numbers:
        if number in winning_numbers:
            correct_numbers += 1
    if correct_numbers == 8:
        return "一等奖"
    elif correct_numbers == 7:
        return "二等奖"
    elif correct_numbers == 6:
        return "三等奖"
    elif correct_numbers == 5:
        return "四等奖"
    else:
        return "未中奖"


def simulate_lottery(winning_numbers, num_tickets=10000):
    """
    模拟购买彩票并计算中奖概率。
    """
    prize_counts = {"一等奖": 0, "二等奖": 0, "三等奖": 0, "四等奖": 0, "未中奖": 0}
    for _ in range(num_tickets):
        user_numbers = random.sample(range(1, 34), 8)
        user_numbers.sort()
        result = check_lottery(user_numbers, winning_numbers)
        prize_counts[result] += 1
    print("模拟结果:")
    for prize, count in prize_counts.items():
        probability = (count / num_tickets) * 100
        print(f"{prize}: {count}次, 概率: {probability:.2f}%")


def main():
    """
    主函数，运行彩票游戏。
    """
    winning_numbers = generate_winning_numbers()
    print("本期中奖号码:", winning_numbers)
    while True:
        try:
            user_input = input("请输入您选择的8个号码（1-33，用空格分隔）：")
            user_numbers = [int(num) for num in user_input.split()]
            if len(user_numbers) != 8 or not all(
                1 <= num <= 33 for num in user_numbers
            ):
                print("错误：请输入8个1-33之间的整数，用空格分隔。")
                continue
            user_numbers.sort()
            break
        except ValueError:
            print("错误：请输入有效的整数。")
        except Exception as e:
            print(f"发生了一个错误: {e}")
    result = check_lottery(user_numbers, winning_numbers)
    print("您的中奖情况:", result)
    simulate_lottery(winning_numbers)


if __name__ == "__main__":
    main()
